package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * 分页查询敏感词
     *
     * @param wmSensitiveDto
     * @return
     */
    @Override
    public PageResponseResult listPage(WmSensitiveDto wmSensitiveDto) {
       // if (wmSensitiveDto == null) return PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        IPage page = new Page<>(wmSensitiveDto.getPage(), wmSensitiveDto.getSize());

        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(wmSensitiveDto.getName() != null,
                WmSensitive::getSensitives, wmSensitiveDto.getName());
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        page = this.page(page, wrapper);

        PageResponseResult result = new PageResponseResult(wmSensitiveDto.getPage(), wmSensitiveDto.getSize(), Integer
                .valueOf(page.getTotal() + ""));

        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        result.setData(page.getRecords());

        return result;
    }

    /**
     * 敏感词添加
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult add(WmSensitive wmSensitive) {
        if (wmSensitive == null || wmSensitive.getSensitives() == null || "".equals(wmSensitive.getSensitives()))
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        WmSensitive sensitive = this.getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (sensitive != null) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        sensitive = new WmSensitive();
        sensitive.setCreatedTime(new Date());
        sensitive.setSensitives(wmSensitive.getSensitives());
        this.save(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
